#pragma once
#include <libwebsockets.h>
#include <jsoncpp/json/json.h>
#include <iostream>
#include <string>
#include <vector>
#include<unordered_map>
#include"Mysqlconnect.hpp"
using namespace std;


class Clientonline
{
public:
    void Bordcast(string &respond)
    {
        Mysqlconnect con;
        con.Connect();
        con.InsertData(respond);
        for(auto&e:_client_map)
        {
            if(e==NULL)
            {
                continue;
            }
            lws_write(e, (unsigned char *)respond.c_str(), respond.length(), LWS_WRITE_TEXT);
        }
        
    }

    void Addclient(struct lws *wsi)
    {
        _client_map.push_back(wsi);
    }

    void Deleteclient(struct lws *wsi)
    {
        for(auto it=_client_map.begin();it!=_client_map.end();it++)
        {
            if(*it==wsi)
            {
                _client_map.erase(it);
                break;
            }
        }
    }
    ~Clientonline()
    {

    }
    private:
    vector<struct lws*> _client_map;
};


Clientonline clientonline;


class Respond
{
public:
    Respond(string type="", string info="", string sender="", string avatar="", string time="" ) 
    : _type(type), _info(info), _sender(sender), _avatar(avatar), _time(time)
    {
    }
    void Serialize(string &out_data)
    {
        Json::Value root;
        root["type"] = _type;
        root["info"] = _info;
        root["sender"] = _sender;
        root["avatar"] = _avatar;
        root["time"] = _time;
        string data = root.toStyledString(); // 序列化
        out_data = data;
        _jsdata = data;
    }

    bool Deserialize(string &out_data)
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingSuccessful = reader.parse(out_data, root);
        if (!parsingSuccessful)
        {
            std::cout << "Failed to parse JSON: " << reader.getFormattedErrorMessages() << std::endl;
            return false;
        }
        _type = root["type"].asString();
        _info = root["info"].asString();
        _sender = root["sender"].asString();
        _avatar = root["avatar"].asString();
       _time = root["time"].asString();
        // Print();
        
        return true;
    }

    void Handle()
    {
        if(_jsdata.empty())
        {
            cout<<"数据为空，不做处理"<<endl;
            return;
        }
        if(_type == "message")
        {
            cout<<"广播消息"<<endl;
            clientonline.Bordcast(_jsdata);
            cout<<"广播结束"<<endl;
        }
    }

    void Print()
    {
        cout<<"----------Begin------------"<<endl;
        cout<<"type: "<<_type<<endl;
        cout<<"info: "<<_info<<endl;
        cout<<"sender: "<<_sender<<endl;
        cout<<"avatar: "<<_avatar<<endl;
        cout<<"time: "<<_time<<endl;
        cout<<"----------End------------"<<endl;
    }

    string Jsdata()
    {
        return _jsdata;
    }
    ~Respond()
    {

    }
private:
    string  _type;
    string _info;
    string _sender;
    string _avatar;
    string _time;
    //序列化后的数据
    string _jsdata;
};






